package com.carkeeper.subject.controller;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.subject.dao.StoreUserMapper;
import com.carkeeper.subject.pojo.Store;
import com.carkeeper.subject.pojo.StoreUser;
import com.carkeeper.subject.service.StoreService;
import com.carkeeper.subject.service.StoreUserService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/storeUser")
@Api(tags = "主体用户")
public class StoreUserController {

    @Autowired
    private StoreUserService storeUserService;

    @ApiOperation("查询服务商")
    @GetMapping("/findPage")
    public Result<PageResult> findPage(@RequestParam Map map) {
        try {
            Page page = storeUserService.findPage(map);
            PageResult<Store> result = new PageResult<>(page.getTotal(), page.getResult());
            return new Result(true, StatusCode.OK, "查询成功", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }

    /**
     * 根据商户id查询旗下用户
     * @param map
     * @return
     */
    @ApiOperation("根据服务商id查询服务商用户")
    @GetMapping("/findUser")
    public Result<PageResult> findQueryByStoreId(@RequestParam Map map) {
        try {
            Page<StoreUser> list = storeUserService.findQueryByStoreId(map);
            PageResult<StoreUser> result = new PageResult<>(list.getTotal(), list.getResult());
            return new Result(true, StatusCode.OK, "查询成功", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }

    /**
     * 添加管理员用户
     *
     * @param storeUser
     */
    @ApiOperation("添加管理员")
    @PostMapping
    public Result addUser(@RequestBody StoreUser storeUser,@RequestParam String storeId) {
        try {
            storeUserService.addUser(storeUser,storeId);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "添加失败");
    }

    /**
     * 解锁与上锁
     * @param storeUserId
     * @return
     */
    @ApiOperation("上锁与解锁")
    @GetMapping("/lock/{storeUserId}")
    public Result lockOrUnlock(@PathVariable("storeUserId") String storeUserId) {
        try {
            storeUserService.lockOrUnlock(storeUserId);
            return new Result(true, StatusCode.OK, "改变成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "改变失败");
    }

    /**
     * 重置密码
     * @param storeUserId
     * @return
     */
    @ApiOperation("重置用户密码")
    @PutMapping
    public Result resetPassword(@RequestParam String storeUserId) {
        try {
            storeUserService.resetPassword(storeUserId);
            return new Result(true, StatusCode.OK, "重置密码成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "重置密码失败");
    }
}
